/**
 * jims
 */
package com.jims.master.modules.sync.service;

import com.jims.master.common.config.Global;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.TreeService;
import com.jims.master.common.utils.CacheUtils;
import com.jims.master.modules.sync.dao.ExamTypeDao;
import com.jims.master.modules.sync.dao.ExamVsClinicDao;
import com.jims.master.modules.sync.entity.ClinicItemDict;
import com.jims.master.modules.sync.entity.ExamType;
import com.jims.master.modules.sync.entity.ExamVsClinic;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.utils.DictUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Service
 * @author 赵宁
 * @version 2017-02-23
 */
@Service
@Transactional(readOnly = true)
public class ExamTypeService extends TreeService<ExamTypeDao, ExamType> {
    @Autowired
    private ExamTypeDao examTypeDao;
    @Autowired
	private OrgDeptDao orgDeptDao;
    @Autowired
	private ExamVsClinicDao examVsClinicDao;

	public ExamType get(String id) {
		return super.get(id);
	}

	public List<ExamType> findList(ExamType examType) {
		return super.findList(examType);
	}

	public Page<ExamType> findPage(Page<ExamType> page, ExamType examType) {
		return super.findPage(page, examType);
	}

	public Page<ExamType> findPageParentIdsLike(Page<ExamType> page, ExamType examType) {
		examType.updateOrgId();
		examType.setPage(page);
		page.setList(dao.findByParentIdsLike(examType));
		return page;
	}

	/**
	 * 保存检查类型
	 * @param examType
	 * @return
	 * @author 赵宁
	 */
	@Transactional(readOnly = false)
	public int save(ExamType examType) {
		if(examType.getParent()!=null&& !"".equals(examType.getParent().getId())&& !"0".equals(examType.getParent().getId())){
			 examType.setLevel("2");
		}else{
			examType.setLevel("1");
		}
		return super.save(examType);
	}

	@Transactional(readOnly = false)
	public int delete(ExamType examType) {
		return super.delete(examType);
	}

	public List<ExamVsClinic> getByParentId(String pId){
		return dao.getByParentId(pId);
	}

    /**
     * 根据上级检查项目查询下级检查项目
     * @param pId
     * @return
     * @Author zhangyao
     */
	public List<ExamType> findByPId(String pId) {
		//查询检查类别子集
		List<ExamType>  examTypeList=examTypeDao.findByPId(pId);
		if(examTypeList.size()>0){
			ExamType examType=examTypeList.get(0);
			ExamVsClinic examVsClinic=new ExamVsClinic();
			examVsClinic.setParentId(examType.getParentId());
			//查询该项目下所有的检查项目
			List<ExamVsClinic> clinicItemDictList =examVsClinicDao.findList(examVsClinic);
			for (ExamType e:examTypeList) {
				List<ExamVsClinic> list=new ArrayList<ExamVsClinic>();
				for (ExamVsClinic v:clinicItemDictList) {
					if(v.getExamId().equals(e.getId())){
						list.add(v);
					}
				}
				//加入诊疗项目
				e.setClinicItemDictList(list);
			}
		}
		return examTypeList;
	}

    /**
     * 根据检查项目查询诊疗项目与执行科室
     * @param examId    检查项目Id
     * @return
     * @Author 陈晓阳
     */
    public List<ClinicItemDict> findByExam(String examId) {
        return this.examTypeDao.findByExam(examId);
    }

	public ExamType getPerformedBy(String id){
		ExamType examType = examTypeDao.getPerformedBy(id);
		return examType;
	}
    /**
     * 根据上级检查项目查询下级检查项目（筛选出病人没有的项目）
     * @return
     * @Author 陈晓阳
     */
    public List<ExamType> tjQueryByPId(String parentId, String patientId, String clinicId) {
        return this.examTypeDao.tjQueryByPId(parentId,patientId,clinicId);
    }

}